package cn.iocoder.yudao.module.blog.service.improve;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.improve.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.improve.BlogImproveDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.improve.BlogImproveConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.improve.BlogImproveMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 课程 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogImproveServiceImpl implements BlogImproveService {

    @Resource
    private BlogImproveMapper improveMapper;

    @Override
    public Long createImprove(BlogImproveCreateReqVO createReqVO) {
        // 插入
        if(createReqVO.getType().equals(2)){
            if(createReqVO.getSkuId() == null){
                throw exception(IMPROVE_SKU_NOT_EXISTS);
            }
        }

        BlogImproveDO improve = BlogImproveConvert.INSTANCE.convert(createReqVO);
        improveMapper.insert(improve);
        // 返回
        return improve.getId();
    }

    @Override
    public void updateImprove(BlogImproveUpdateReqVO updateReqVO) {
        // 校验存在
        validateImproveExists(updateReqVO.getId());
        // 更新
        BlogImproveDO updateObj = BlogImproveConvert.INSTANCE.convert(updateReqVO);
        improveMapper.updateById(updateObj);
    }

    @Override
    public void deleteImprove(Long id) {
        // 校验存在
        validateImproveExists(id);
        // 删除
        improveMapper.deleteById(id);
    }

    private void validateImproveExists(Long id) {
        if (improveMapper.selectById(id) == null) {
            throw exception(IMPROVE_NOT_EXISTS);
        }
    }

    @Override
    public BlogImproveDO getImprove(Long id) {

        BlogImproveDO improveDO = improveMapper.selectById(id);
        if(improveDO != null){
            improveDO.setReadNum(improveDO.getReadNum() + 1);
            improveMapper.updateById(improveDO);
        }

        return improveMapper.selectById(id);
    }

    @Override
    public List<BlogImproveDO> getImproveList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return improveMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogImproveDO> getImprovePage(BlogImprovePageReqVO pageReqVO) {

        return improveMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogImproveDO> getImproveList(BlogImproveExportReqVO exportReqVO) {
        return improveMapper.selectList(exportReqVO);
    }

    @Override
    public BlogImproveDO getImproveBySkuId(Long skuId){
        return improveMapper.selectOne(BlogImproveDO::getSkuId, skuId);
    }

}
